<?php

namespace addons\shopro\controller\supervise;

use addons\shopro\controller\Common;
use app\admin\model\shopro\lucky\Order;
use app\admin\model\shopro\lucky\Record;
use app\admin\model\shopro\user\User;
use app\admin\model\shopro\Withdraw;

class Supervise extends Common
{


    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function getInfo()
    {
        $user = auth_user();
        if(!$user->is_admin || $user->is_admin!=1){
            $this->error('无权限');
        }

        $total_withdraw=Withdraw::where('status',2)->sum('amount');
        $total_recharge=Record::where('status',1)->sum('account');
        $order_count=Order::where('status',0)->count();
        $lucky_user_count=\app\admin\model\shopro\lucky\User::where('stutas',0)->count();
        $user_count=User::count();
        $total_userinfo=User::field('sum(money) as total_money,sum(commission) as total_commission,sum(score) as total_score,sum(tokens) as total_tokens')->find();

        $data=[
            'total_withdraw'=>$total_withdraw,
            'total_recharge'=>$total_recharge,
            'order_count'=>$order_count,
            'lucky_user_count'=>$lucky_user_count,
            'user_count'=>$user_count,
            'total_userinfo'=>$total_userinfo
        ];

        $this->success('ok',$data);

    }

    public function getLog()
    {
        $user = auth_user();
        if(!$user->is_admin || $user->is_admin!=1){
            $this->error('无权限');
        }

        //$type = $this->request->param('type', 'money');
        $tab = $this->request->param('tab', 'all');
        $list_rows = $this->request->param('list_rows', 10);
        $date = $this->request->param('date/a');
        $list = [];

        switch ($tab) {
            case 'income':
                //$where['amount'] = ['>', 0];
                $list=Record::with('user')->where('record.status',1)->whereTime('record.createtime', 'between', $date)->order('record.createtime', 'desc')->paginate($list_rows);

                foreach ($list as $row) {
                    $row->visible(['id','order_num','account','createtime']);
                    $row->visible(['user']);
                    $row->getRelation('user')->visible(['username','nickname']);
                }
                break;
            case 'expense':
                $list=Withdraw::with('user')->where('status',2)->whereTime('createtime', 'between', $date)->order('createtime', 'desc')->paginate($list_rows);

                foreach ($list as $row) {
                    $row->visible(['id','currency_type','amount','des_tokens','withdraw_sn','createtime']);
                    $row->visible(['user']);
                    $row->getRelation('user')->visible(['username','nickname']);
                }
                break;
        }
        $income=Record::where('status',1)->whereTime('createtime', 'between', $date)->sum('account');
        $expense=Withdraw::where('status',2)->whereTime('createtime', 'between', $date)->sum('amount');
        $data=[
            'list'=>$list,
            'income'=>$income,
            'expense'=>$expense
        ];

        $this->success('ok',$data);

    }

}